#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import Flask, request, jsonify
from service.templateService import TemplateService
from utils import commons
from utils.response_code import RET


class TemplateOtherResource(Resource):
    @classmethod
    def add_ppt_template(cls):
        """
        添加 PPT 模板接口
        """
        parser = reqparse.RequestParser()
        parser.add_argument('user_id', location='form', required=True, help='user_id 参数类型不正确或缺失')
        parser.add_argument('template_name', location='form', required=True, help='template_name 参数类型不正确或缺失')
        parser.add_argument('cover_pic', location='form', required=False, help='cover_pic 参数类型不正确或缺失')
        args = parser.parse_args()
        args = commons.put_remove_none(**args)
        # 获取上传的文件
        args['file'] = request.files.get('file')
        if not args['file']:
            return jsonify({'code': RET.PARAMERR, 'message': "缺少上传的文件", 'data': {}})

        res = TemplateService.add_ppt_template(**args)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def update_ppt_template(cls):
        """
        更新 PPT 模板接口
        """
        parser = reqparse.RequestParser()
        parser.add_argument('user_id', location='form', required=True, help='user_id 参数类型不正确或缺失')
        parser.add_argument('template_id', location='form', required=True, help='template_id 参数类型不正确或缺失')
        parser.add_argument('template_name', location='form', required=True, help='template_name 参数类型不正确或缺失')
        parser.add_argument('cover_pic', location='form', required=False, help='cover_pic 参数类型不正确或缺失')
        args = parser.parse_args()

        # 获取上传的文件
        args['file'] = request.files.get('file')
        if not args['file']:
            return jsonify({'code': RET.PARAMERR, 'message': "缺少上传的文件", 'data': {}})

        res = TemplateService.update_ppt_template(**args)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def look_ppt_templates(cls):
        """
        查看 PPT 模板接口
        """
        parser = reqparse.RequestParser()
        parser.add_argument('page', location='args', type=int, default=1, help='page 参数类型不正确')
        parser.add_argument('per_page', location='args', type=int, default=10, help='per_page 参数类型不正确')
        args = parser.parse_args()

        res = TemplateService.look_ppt_templates(**args)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def get_ppt_templates(cls):
        """
            PPT文件流
        """
        parser = reqparse.RequestParser()
        parser.add_argument('location', location='args', required=True, help='location 参数类型不正确或缺失')
        args = parser.parse_args()

        res = TemplateService.get_ppt_template(**args)
        return res
